package com.base.moduleUser.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.core.common.utils.UserContext;
import com.base.core.domain.StandardLocation;
import com.base.core.domain.UserTrajectory;
import com.base.moduleUser.entity.dto.RecordLocationDto;
import com.base.moduleUser.mapper.StandardLocationMapper;
import com.base.moduleUser.mapper.UserMapper;
import com.base.moduleUser.service.StandardLocationService;
import com.base.moduleUser.service.UserFrequentLocationService;
import com.base.moduleUser.service.UserTrajectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author XinyuZhao
* @description 针对表【standard_location(标准地址表)】的数据库操作Service实现
* @createDate 2025-07-14 15:34:45
*/
@Service
public class StandardLocationServiceImpl extends ServiceImpl<StandardLocationMapper, StandardLocation>
    implements StandardLocationService{

    @Autowired
    private UserTrajectoryService userTrajectoryService;
    @Autowired
    private UserFrequentLocationService userFrequentLocationService;

    @Override
    public void recordLocation(RecordLocationDto recordLocationDto) {
        //条件查询标准库是否存在该地址
        StandardLocation standardLocation = this.lambdaQuery()
                .eq(StandardLocation::getStandardName,recordLocationDto.getAddress())
                .one();
        //如果不存在，则新增
        if(standardLocation == null){
            standardLocation = new StandardLocation();
            standardLocation.setLocationPoint(recordLocationDto.getLocationPoint());
            standardLocation.setStandardName(recordLocationDto.getAddress());
            standardLocation.setAddress(recordLocationDto.getAddress());
            this.save(standardLocation);
        }
        //关联用户轨迹
        UserTrajectory userTrajectory = new UserTrajectory();
        userTrajectory.setUserId(UserContext.getCurrentUser().getId());
        userTrajectory.setStandardLocationId(standardLocation.getId());
        userTrajectory.setArrivalAt(new Date());
        userTrajectory.setAccuracy(recordLocationDto.getAccuracy());
        userTrajectory.setSpeed(0.0);
        userTrajectoryService.save(userTrajectory);
    }
}




